package com.tky.conference.implInterfaces;

import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.tky.conference.base.UrlConstant;
import com.tky.conference.dao.ComeetingInfo;
import com.tky.conference.dao.LoginInfo;
import com.tky.conference.entity.FailRequest;
import com.tky.conference.interfaces.ComeetingInfoInter;
import com.tky.conference.interfaces.HttpResultListener;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cll on 2015/12/7.
 */
public class ComeetingInfoImpl implements ComeetingInfoInter {
    private String TAG = "ComeetingInfoImpl";
    private ComeetingInfo comeetingInfo;
    private FailRequest failRequestobj;
    private HttpResultListener httpResultListener;

    public ComeetingInfoImpl(HttpResultListener httpResultListener) {
        this.httpResultListener = httpResultListener;
    }
    @Override
    public void getComeetingInfo(String account, String type, String pageNo) {
        AsyncHttpClient client = new AsyncHttpClient();
        RequestParams params=new RequestParams();
        client.setTimeout(10000);
        params.put("account", account);
        params.put("type", type);
        params.put("pageNo", pageNo);
        client.setTimeout(UrlConstant.TIME_OUT);
        client.get(UrlConstant.MYCONFER,params,  new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                System.out.println("success");
                Log.i(TAG, "succ");
                JSONArray jsonArr;
                JSONObject jsonObject;
                JSONObject failjsonobj;
                String data = null;
                try {
                    String str = new String(bytes, "UTF-8");
                    Log.i(TAG, str);
                    jsonObject= new JSONObject(str);
                    String s = jsonObject.getString("status");
                    Log.i(TAG, s);
                    if (Integer.parseInt(s) >0) {
                        failRequestobj = new FailRequest();
                        failRequestobj.setStatus(jsonObject.getString("status"));
                        failRequestobj.setMsg(jsonObject.getString("msg"));
                        httpResultListener.onResult(failRequestobj);
                    }else{
                        String dataobj=jsonObject.getString("data");
                        Log.i(TAG, dataobj);
                        JSONObject datao=new JSONObject(dataobj);
                        JSONArray dataarr=new JSONArray(datao.getString("list"));
                        Log.i(TAG, dataarr.toString());
                        ArrayList<ComeetingInfo> arr=new ArrayList<ComeetingInfo>();
                        for (int j = 0; j < dataarr.length(); j++) {
                            data = dataarr.get(j).toString();
                            jsonObject = new JSONObject(data);
                            comeetingInfo=new ComeetingInfo();
                            comeetingInfo.setId(jsonObject.getString("id"));
                            comeetingInfo.setEtime(jsonObject.getString("etime"));
                            comeetingInfo.setFzr(jsonObject.getString("fzr"));
                            comeetingInfo.setHydd(jsonObject.getString("hydd"));
                            comeetingInfo.setHysmc(jsonObject.getString("hysmc"));
                            comeetingInfo.setHyzt(jsonObject.getString("hyzt"));
                            comeetingInfo.setStatus(jsonObject.getString("status"));
                            comeetingInfo.setStime(jsonObject.getString("stime"));
                            comeetingInfo.setZbbm(jsonObject.getString("zbbm"));
                            comeetingInfo.setChry(jsonObject.getString("chry"));
                            comeetingInfo.setJlr(jsonObject.getString("jlr"));
                            comeetingInfo.setSqr(jsonObject.getString("sqr"));
                            if(jsonObject.getString("message").equals("")){
                                Log.i("消息发送为空","okokokok");
                                comeetingInfo.setMessage(" ");
                            }else{
                                comeetingInfo.setMessage(jsonObject.getString("message"));
                            }
                            if(jsonObject.getString("hyms").equals("")){
                                Log.i("会议描述为空","okokokok");
                                comeetingInfo.setHyms(" ");
                            }else {
                                comeetingInfo.setHyms(jsonObject.getString("hyms"));
                            }

                            comeetingInfo.setUseflag(jsonObject.getString("useflag"));
                            comeetingInfo.setChryid(jsonObject.getString("chryid"));
                            comeetingInfo.setFzrid(jsonObject.getString("fzrid"));
                            comeetingInfo.setCreateat(jsonObject.getString("createat"));
                            comeetingInfo.setHysid(jsonObject.getString("hysid"));
                            comeetingInfo.setJlrid(jsonObject.getString("jlrid"));
                            comeetingInfo.setOrgid(jsonObject.getString("orgid"));
                            arr.add(comeetingInfo);
                        }
                        httpResultListener.onResult(arr);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                try {
                    System.out.println("fail");
                    try {
                        failRequestobj = new FailRequest();
                        failRequestobj.setStatus("-1");
                        failRequestobj.setMsg("网络异常");
                        httpResultListener.onResult(failRequestobj);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


            @Override
            public void onStart() {
                super.onStart();
                httpResultListener.onStartRequest();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                httpResultListener.onFinish();
            }
        });
    }
}
